<?php

/**
 * Controller_arrange
 * 各个管理增删改查
 */

class Controller_arrange extends MyApp {
	public function __construct()
	{

		parent::__construct();

		$this->model->semester   = YOU::loadModel('semester');
		$this->model->excel      = YOU::loadModel('excel');
		$this->model->semester   = YOU::loadModel('semester');
		$this->model->department = YOU::loadModel('department');

	}
	/** 首页 **/
	public function actionIndex()
	{

	}
	/** -----------------------------学期工作周历表--------------------------------- **/

	/** 学期工作周历表导出 **/
	public function actionworkWeekIn()
	{

		$semester_id	= $this->get->semester_id;
		$semester_data = $this->model->semester->getInfo($semester_id);

		if(empty($semester_id)){
			// 如果没有get传值，就或的当前学期的id
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
		}

		$department = $this->model->department->getIni();

		$work_lists = $this->db->fetchAll("SELECT * FROM `tbl_week_work` WHERE semester_id={$semester_id} ORDER BY work_week ASC");

		foreach ($work_lists as $key => $value){
			// 负责部门
			$work_department = unserialize($value['work_department']);
			$part = "";
			foreach((array)$work_department as $k => $v){
				// 部门名称
				$part .= $department[$v].",";
			}

			// 周开始结束时间
			$week = $this->model->semester->getWeekInfo($semester_id, $value['work_week']);

			$info = array(
			"0" => $value['work_week'],
			"1" => "{$week['start_time']} - {$week['end_time']}",
			"2" => $value['work_content'],
			"3" => $part.$value['work_rests'],
			"4" => $value['work_remark'],
			);

			$data_lists[] = $info;

		}

		// 模板标题
		$this->model->excel->excel_ini['workweek']['title'] = "{$semester_data['semester_name']}学期工作周历表";
		$this->model->excel->outData("workweek1", $data_lists);

	}

	/** 学期工作周历表列表 **/
	public function actionSemesterWeekList ()
	{
		// 获得所有学期
		$semester_lists = $this->model->semester->getIni();
		$semester_id	= $this->get->semester_id;

		if(empty($semester_id)){
			// 如果没有get传值，就或的当前学期的id
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
		}

		// 学期
		$semester_data = $this->model->semester->getInfo($semester_id);

		// 部门
		$department_lists = $this->db->fetchAll("SELECT department_id,department_name FROM `tbl_department`");
		foreach($department_lists as $key => $value){

			$department[$value['department_id']] = $value['department_name'];

		}
		// 本学期周表
		$week_lists = $this->db->fetchAll("SELECT * FROM `tbl_week` WHERE semester_id={$semester_id}");

		// 搜索
		$where = " WHERE 1=1";

		if (!empty($semester_id)){
			$where .= " AND semester_id= '{$semester_id}'";
			$this->tpl->assign("semester_id", $semester_id);
		}else{
			$where .= " AND semester_id= '{$semester_id}'";
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_week_work` {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$work_lists = $this->db->fetchAll("SELECT * FROM `tbl_week_work` {$where} ORDER BY work_week ASC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($work_lists as $key=>$value ){
			// 负责部门
			$work_department = unserialize($value['work_department']);
			foreach((array)$work_department as $k => $v){
				// 部门名称
				$work_lists[$key]['department_name'][$k] = $department[$v];
			}
			foreach($week_lists as $k=>$v){
				// 每周的开始时间和结束时间
				if($value['work_week']==$v['week_number']){
					$work_lists[$key]['start_time'] = $v['start_time'];
					$work_lists[$key]['end_time'] = $v['end_time'];

				}
			}
			//  内容换行
			$work_lists[$key]['work_content'] = nl2br($value['work_content']);
			// 备注换行
			$work_lists[$key]['work_remark'] = nl2br($value['work_remark']);
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("work_lists", $work_lists);
		$this->tpl->assign("semester_data", $semester_data);

		$this->tpl->assign("semester_lists", $semester_lists);

		$this->tpl->display('arrange/m.work.list.tpl.php');
	}
	/** 学期周历表添加 **/
	public function actionSemesterWeekAdd()
	{

		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$week_num		= $this->post->week_num;

		// 当前学期的周次

		$week_lists = $this->model->semester->getWeekAll($semester_id);

		// 所有的部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");

		if ($_POST){

			$work_week				= $this->post->work_week;
			$work_content     		= $this->post->work_content;
			$department_id   	    = $this->post->department_id;
			$work_rests   			= $this->post->work_rests;
			$work_remark   			= $this->post->work_remark;
			$go   			        = $this->post->go;

			// 序列化部门id
			$department_id = serialize($department_id);
			// 查询当前学期所有的周
			$work_lists = $this->db->fetchAll("SELECT * FROM `tbl_week_work` WHERE work_week = '{$work_week}' AND semester_id = '{$semester_id}'");
			// 重复的周次不能添加

			if(!empty($work_lists)){
				Helper_Client::alert("本学期周次存在，请重新选择周次");
			}
			if(!empty($go)){

				if($work_lists>$end_week){
					Helper_Client::alert("已经是本学期最后一个周，无法继续添加");
				}

				// 写入数据库
				$info = array(

				"work_week"				=> $work_week,
				"work_content"   		=> $work_content,
				"work_department"       => $department_id,
				"work_rests"   			=> $work_rests,
				"work_remark"  			=> $work_remark,
				"semester_id"  			=> $semester_id,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_insert("tbl_week_work", $info);

				// 跳转
				Helper_Client::location("添加成功", "index.php?a=arrange.semesterweekadd&semester_id={$semester_id}");
				$work_week = $work_week+1;

			}else{
				// 写入数据库
				$info = array(

				"work_week"				=> $work_week,
				"work_content"   		=> $work_content,
				"work_department"       => $department_id,
				"work_rests"   			=> $work_rests,
				"work_remark"  			=> $work_remark,
				"semester_id"  			=> $semester_id,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_insert("tbl_week_work", $info);

				// 跳转
				Helper_Client::location("添加成功", "index.php?a=arrange.semesterweeklist&semester_id={$semester_id}");
			}
		}

		$this->tpl->assign("hourweek_lists", $hourweek_lists);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("work_week", $work_week);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->display('arrange/work.add.tpl.php');
	}
	/** 学期周修改 **/
	public function actionSemesterWeekUpdate()
	{

		$semester_id	= $this->get->semester_id;
		$work_id		= $this->get->work_id;
		$week_num		= $this->post->week_num;
		// 获取当前的学期
		$semester_lists = $this->db->fetchRow("SELECT * FROM `tbl_semester` WHERE semester_id={$semester_id}");
		// 所有的部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");
		// 获得对应的周表
		$work_data = $this->db->fetchRow("SELECT * FROM `tbl_week_work` WHERE work_id={$work_id}");
		// 部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");
		$work_other = unserialize($work_data['work_department']);
		foreach($department_lists as $key => $value){
			if(in_array($value['department_id'], $work_other)){

				$department_lists[$key]['is_check'] = '1';
			}

		}

		if (empty($work_data)){
			Helper_Client::alert("当前记录不存在");
		}

		if ($_POST){

			$work_content     		= $this->post->work_content;
			$department_id   	    = $this->post->department_id;
			$work_rests   			= $this->post->work_rests;
			$work_remark   			= $this->post->work_remark;
			$go   			        = $this->post->go;

			// 写入数据库
			$info = array(

			"work_content"   		=> $work_content,
			"work_department"       => serialize($department_id),
			"work_rests"   			=> $work_rests,
			"work_remark"  			=> $work_remark,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_week_work", $info, "work_id='{$work_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=arrange.semesterweeklist&semester_id={$semester_id}");
		}

		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("arrange_data", $arrange_data);
		$this->tpl->assign("work_data", $work_data);
		$this->tpl->display('arrange/work.update.tpl.php');
	}
	/** 学期周删除 **/
	public function actionSemesterWeekDel()
	{
		$semester_id	= $this->get->semester_id;
		$work_id		= $this->get->work_id;
		$this->db->query("DELETE  FROM `tbl_week_work` WHERE work_id='{$work_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=arrange.semesterweeklist&semester_id={$semester_id}");
	}
	/** ----------------------------学校每周工作安排---------------------------------- **/

	/** 每周工作导出 **/
	public function actionPlanOutData()
	{


		// 当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 取部门名字
		$department = $this->model->department->getIni();
		// 当前周
		$plan_week = $this->get->plan_week;

		// 查询条件
		$where = " WHERE 1=1 AND semester_id='{$semester_id}' ";

		if(!empty($plan_week)){
			// 周次
			$where .= " AND plan_week ='{$plan_week}'";
			$this->tpl->assign("plan_week", $plan_week);
		}

		// 查询结果
		$plan_lists = $this->db->fetchAll("SELECT * FROM `tbl_plan` {$where}");

		foreach($plan_lists as $key => $value){

			// 获取星期几
			$plan_lists[$key]['plan_day'] = $this->week_ini[$value['plan_day']];
			// 获取周次
			$week_num = $value['plan_week'];
			// 部门
			$arrange_other = unserialize($value['plan_department']);

			$part = array();
			if(!empty($arrange_other)){
				foreach($arrange_other as $k => $v){
					// 部门名称
					$part[] = $department[$v];
				}
			}
			$part[] = $value['plan_rests'];
			$part = implode(",", $part);

			$info = array(
			"0" => $value['plan_day'],
			"1" => $value['start_time'],
			"2" => $value['plan_content'],
			"3" => $part,
			"4" => $value['plan_remark'],
			);


			$data_lists[] = $info;


		}

		// 模板标题
		$this->model->excel->excel_ini['plan']['title'] = "学校{$semester_data['semester_name']}第{$plan_week}周工作安排 ";
		$this->model->excel->outData("plan", $data_lists);

	}



	/** 学校每周工作安排列表 **/
	public function actionPlanList ()
	{

		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 取部门名字
		$department = $this->model->department->getIni();
		// 当前周

		$plan_week = $this->get->plan_week;
        $this->tpl->assign("plan_week",$plan_week);
		// 获取上一周开始和结束时间
		//	$last_start = $this->last_start;
		//	$last_end = $this->last_end;
		// 获取所有不重复周次
		$week_lists = $this->db->fetchAll("SELECT DISTINCT `plan_week` FROM `tbl_plan` WHERE semester_id='{$semester_id}'");

		// 查询条件
		$plan_week	= $this->get->plan_week;

		$where = " WHERE 1=1 AND semester_id='{$semester_id}' ";

		if(!empty($plan_week)){
			// 周次
			$where .= " AND plan_week ='{$plan_week}'";
			$this->tpl->assign("plan_week", $plan_week);
		}

		// 查询结果
		$plan_lists = $this->db->fetchAll("SELECT * FROM `tbl_plan` {$where}");

		//if(!empty($plan_week) ){

		// 当前周次,获取当前周的开始时间和结束时间
		//$now_start = $this->now_start;
		//	$now_end   = $this->now_end;
		//$plan_lists = $this->db->fetchAll("SELECT * FROM `tbl_plan` WHERE plan_week={$plan_week}");


		foreach($plan_lists as $key=>$value){
			// 获取星期几
			$plan_lists[$key]['plan_day'] = $this->week_ini[$value['plan_day']];
			// 获取周次
			$week_num = $value['plan_week'];
			// 部门
			$arrange_other = unserialize($value['plan_department']);
			if(is_array($arrange_other)){
			foreach($arrange_other as $k => $v){
				// 部门名称
				$plan_lists[$key]['department_name'][$k] = $department[$v];
			}
			}
		}
		/*
		//}else{
		// 获取本地时间的上周数据
		//		$plan_lists = $this->db->fetchAll("SELECT * FROM `tbl_plan` WHERE start_time>='{$last_start}' AND start_time<='{$last_end}'");
		//	foreach($plan_lists as $key=>$value){
		// 获取星期几
		//		$plan_lists[$key]['plan_day'] = $this->week_ini[$value['plan_day']];
		// 获取周次
		//		$week_num = $value['plan_week'];
		}
		//}
		*/
		$this->tpl->assign("semester_id", $semester_id);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("plan_lists", $plan_lists);
		$this->tpl->assign("week_num", $week_num);
		$this->tpl->display('arrange/m.plan.list.tpl.php');
	}
	/** 学期周历表添加 **/
	public function actionPlanAdd()
	{
		$semester_id	= $this->get->semester_id;

		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$week_num		= $this->post->week_num;

		if ($_POST){

			$start_time			= $this->post->start_time;
			$plan_week			= $this->post->plan_week;

			if(empty($plan_week)){
				Helper_Client::alert("请填写周次");
			}

			if(empty($start_time)){
				Helper_Client::alert("请填写日期");
			}

			// 判断周次是否存在
			$plan_data    = $this->db->fetchRow("SELECT * FROM `tbl_plan` WHERE plan_week='{$plan_week}' AND semester_id='{$semester_id}' ");
			if(!empty($plan_data)){
				Helper_Client::alert("本周已经存在，请重新填写");
			}
			$i=1;
			//  存7条数据（星期1-星期7）
			for($i=1;$i<8;$i++){
				// 星期几
				$plan_day =$i;
				// 时间(星期2-星期6)
				if($i > 1){
					$start_time = date("Y-m-d", strtotime("+1 day", strtotime($start_time)));
				}
				$plan_content			= $_POST["plan_content_{$i}"];
				$department_id			= $_POST["department_id_{$i}"];
				$plan_rests				= $_POST["plan_rests_{$i}"];
				$plan_remark			= $_POST["plan_remark_{$i}"];

				// 写入数据库
				$info = array(
				"semester_id"			=> $semester_id,
				"start_time"			=> $start_time,
				"plan_week"				=> $plan_week,
				"plan_day"				=> $plan_day,
				"plan_content"   		=> $plan_content,
				"plan_department"       => serialize($department_id),
				"plan_rests"   			=> $plan_rests,
				"plan_remark"  			=> $plan_remark,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);

				$this->db->do_insert("tbl_plan", $info);
			}

			if($work_lists > $end_week){
				Helper_Client::alert("已经是本学期最后一个周，无法继续添加");
			}

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=arrange.planlist&plan_week={$plan_week}");

		}
		// 所有的部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");

		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("plan_week", $plan_week);
		$this->tpl->display('arrange/plan.add.tpl.php');
	}
	/** 学期周历表修改 **/
	public function actionPlanUpdate()
	{
		$semester_id = $this->get->semester_id;
		$plan_week   = (int)$this->get->plan_week;
		$plan_lists = $this->db->fetchAll("SELECT * FROM `tbl_plan` WHERE `plan_week`='{$plan_week}' AND semester_id='{$semester_id}'");
		if(empty($plan_lists)){
			Helper_Client::alert("本周不存在");
		}

		if ($_POST){
			$start_time				= $this->post->start_time;

			$i=1;
			//  存7条数据（星期1-星期7）
			for($i=1;$i<8;$i++){
				// 星期几
				$plan_day =$i;
				// 时间(星期2-星期6)
				if($i > 1){
					$start_time = date("Y-m-d", strtotime("+1 day", strtotime($start_time)));
				}

				$plan_content			= $_POST["plan_content_{$i}"];
				$department_id			= $_POST["department_id_{$i}"];
				$plan_rests				= $_POST["plan_rests_{$i}"];
				$plan_remark			= $_POST["plan_remark_{$i}"];

				// 写入数据库
				$info = array(

				"start_time"			=> $start_time,
				"plan_content"   		=> $plan_content,
				"plan_department"       => serialize($department_id),
				"plan_rests"   			=> $plan_rests,
				"plan_remark"  			=> $plan_remark,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);

				$a = $this->db->do_update("tbl_plan", $info, "plan_week='{$plan_week}' AND plan_day='{$i}' AND semester_id='{$semester_id}'");

			}

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=arrange.planlist&plan_week={$plan_week}");
		}
		// 所有的部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");

		foreach($plan_lists as $key => $value){
			$plan_lists[$key]['day_note'] = $this->week_ini[$value['plan_day']];
			$arrange_other = unserialize($value['plan_department']);

			// 默认部门选对号
			foreach($department_lists as $k => $v){
				$plan_lists[$key]['department_lists'][$v['department_id']]['department_name'] = $v['department_name'];
				if(!empty($arrange_other)){
					if(in_array($v['department_id'], $arrange_other)){
						$plan_lists[$key]['department_lists'][$v['department_id']]['is_check'] = 1;
					}
				}
			}
		}

		$this->tpl->assign("plan_lists", $plan_lists);
		$this->tpl->assign("plan_week", $plan_week);
		$this->tpl->assign("department_lists", $department_lists);

		$this->tpl->display('arrange/plan.update.tpl.php');
	}
	/** 学期周历表删除 **/
	public function actionPlanDel()
	{
		$plan_week = $this->get->plan_week;
		$semester_id = $this->get->semester_id;

		$this->db->query("DELETE  FROM `tbl_plan` WHERE plan_week='{$plan_week}' AND semester_id='{$semester_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=arrange.planlist&now_week={$plan_week}");
	}

	/** ----------------------------- 学校每天工作安排----------------------------------- **/


	/** 每天导出 **/
	public function actionWorkDayIn()
	{


		$department = $this->model->department->getIni();

		// 日常工作 arrange_type =3
		$hourday_lists = $this->db->fetchAll("SELECT * FROM `tbl_arrange`  WHERE arrange_type=3  ORDER BY arrange_id DESC ");

		foreach($hourday_lists as $key => $value){
			// 负责部门
			$work_department = unserialize($value['arrange_other']);
			$part = "";
			foreach((array)$work_department as $k => $v){
				// 部门名称
				$part .= $department[$v].",";
			}

			$week = date('w',strtotime($value['start_time']));

			$info = array(
			"0" => $value['start_time'],
			"1" => $week,
			"2" => $value['arrange_content'],
			"3" => $part.$value['arrange_rest'],
			"4" => $value['arrange_remark'],
			);
			$data_lists[] = $info;
		}

		// 模板标题
		$this->model->excel->excel_ini['workweek']['title'] = "学校日常工作布置表";
		$this->model->excel->outData("workweek", $data_lists);
	}

	/** 学校日常工作布置列表 **/
	public function actionHourDayList ()
	{
		$where = "";

		$start_time = $this->get->start_time;
		if (!empty($start_time)){
			$where .= "  AND start_time >= '{$start_time}' ";
			$this->tpl->assign("start_time", $start_time);
		}
		$end_time = $this->get->end_time;
		if (!empty($end_time)){
			$where .= "  AND start_time <= '{$end_time}+1' ";
			$this->tpl->assign("end_time", $end_time);
		}


		$department_lists = $this->db->fetchAll("SELECT department_id,department_name FROM `tbl_department`");
		foreach($department_lists as $key => $value){

			$department[$value['department_id']] = $value['department_name'];

		}
		/*// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_arrange` WHERE arrange_type =4 {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}*/
		// 日常工作 arrange_type =3
		//if ($where !="") {
			$hourday_lists = $this->db->fetchAll("SELECT * FROM `tbl_arrange`  WHERE arrange_type=3 {$where} ORDER BY arrange_id DESC");
		//}
		

		foreach($hourday_lists as $key=>$value){
			// 时间转换
			$hourday_lists[$key]['start_time'] =date('Y-m-d',strtotime($value['start_time']));
			// 周次
			$hourday_lists[$key]['week'] = $this->week_en[date('w', strtotime($value['start_time']))];
			// 内容控制字数
			$hourday_lists[$key]['arrange_content'] 	= mb_substr($value['arrange_content'],0,10,'utf8');
			if(strlen($hourday_lists[$key]['arrange_content'])>10){
				$hourday_lists[$key]['arrange_content']=mb_substr($hourday_lists[$key]['arrange_content'],0,10,utf8).'...';
			}

			// 标题控制字数
			$hourday_lists[$key]['arrange_title'] 	= mb_substr($value['arrange_title'],0,10,'utf8');
			if(strlen($hourday_lists[$key]['arrange_title'])>10){
				$hourday_lists[$key]['arrange_title']=mb_substr($hourday_lists[$key]['arrange_title'],0,10,utf8).'...';
			}
			// 备注控制字数
			$hourday_lists[$key]['arrange_remark'] 	= mb_substr($value['arrange_remark'],0,10,'utf8');
			if(strlen($hourday_lists[$key]['arrange_remark'])>10){
				$hourday_lists[$key]['arrange_remark']=mb_substr($hourday_lists[$key]['arrange_remark'],0,10,utf8).'...';
			}
			// 部门
			$arrange_other = unserialize($value['arrange_other']);

			foreach($arrange_other as $k => $v){

				// 部门名称
				$hourday_lists[$key]['department_name'][$k] = $department[$v];

			}
		}
/*
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));
*/
		$this->tpl->assign("hourday_lists", $hourday_lists);
		$this->tpl->display('arrange/m.hourday.list.tpl.php');
	}

	/** 学校日常工作布置添加 **/
	public function actionHourDayAdd()
	{
		// 部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");

		if ($_POST){

			$start_time   			= $this->post->start_time;
			$arrange_content     	= $this->post->arrange_content;
			$arrange_other   	    = $this->post->arrange_other;
			$arrange_rest   	    = $this->post->arrange_rest;
			$arrange_remark   	    = $this->post->arrange_remark;
			$arrange_title   	    = $this->post->arrange_title;
			// 序列化部门
			$arrange_other			= serialize($arrange_other);

			// 写入数据库
			$info = array(

			"arrange_title"         => $arrange_title,
			"arrange_content"   	=> $arrange_content,
			"arrange_other"  		=> $arrange_other,
			"arrange_rest"  		=> $arrange_rest,
			"arrange_remark"  		=> $arrange_remark,
			"start_time"  			=> $start_time,
			// 3 = 日常工作
			"arrange_type"  		=> 3,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_arrange", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=arrange.HourdayList");
		}

		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->display('arrange/m.hourday.add.tpl.php');
	}
	/** 学校日常工作布置修改 **/
	public function actionHourDayUpdate()
	{

		$arrange_id   = (int)$this->get->arrange_id;
		$arrange_data = $this->db->fetchRow("SELECT * FROM `tbl_arrange` WHERE `arrange_id`='{$arrange_id}'");
		// 部门
		$department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");
		$arrange_other = unserialize($arrange_data['arrange_other']);
		// 默认部门选对号
		foreach($department_lists as $key => $value){
			if(in_array($value['department_id'], $arrange_other)){

				$department_lists[$key]['is_check'] = '1';
			}

		}

		if (empty($arrange_data)){
			Helper_Client::alert("日常工作内容不存在");
		}

		if ($_POST){

			$start_time   			= $this->post->start_time;
			$arrange_content     	= $this->post->arrange_content;
			$arrange_other   	    = $this->post->arrange_other;
			$arrange_rest   	    = $this->post->arrange_rest;
			$arrange_remark   	    = $this->post->arrange_remark;
			$arrange_title   	    = $this->post->arrange_title;

			// 序列化部门
			$arrange_other			= serialize($arrange_other);


			// 写入数据库
			$info = array(

			"arrange_title"			=> $arrange_title,
			"start_time"			=> $start_time,
			"arrange_content"   	=> $arrange_content,
			"arrange_other"  		=> $arrange_other,
			"arrange_rest"  		=> $arrange_rest,
			"arrange_remark"  		=> $arrange_remark,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_arrange", $info, "arrange_id='{$arrange_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=arrange.HourdayList");
		}

		$this->tpl->assign("department_lists", $department_lists);
		$this->tpl->assign("arrange_data", $arrange_data);
		$this->tpl->display('arrange/m.hourday.update.tpl.php');
	}
    /** 学校日常工作info **/
    public function actionHourDayinfo()
    {

        $arrange_id   = (int)$this->get->arrange_id;
        $arrange_data = $this->db->fetchRow("SELECT * FROM `tbl_arrange` WHERE `arrange_id`='{$arrange_id}'");
        // 部门
        $department_lists = $this->db->fetchAll("SELECT * FROM `tbl_department`");
        $arrange_other = unserialize($arrange_data['arrange_other']);
        // 默认部门选对号
        foreach($department_lists as $key => $value){
            if(in_array($value['department_id'], $arrange_other)){

                $department_lists[$key]['is_check'] = '1';
            }

        }

        if (empty($arrange_data)){
            Helper_Client::alert("日常工作内容不存在");
        }



        $this->tpl->assign("department_lists", $department_lists);
        $this->tpl->assign("arrange_data", $arrange_data);
        $this->tpl->display('arrange/hourday.info.tpl.php');
    }
	/** 学期天删除 **/
	public function actionHourDayDel()
	{
		$arrange_id = $this->get->arrange_id;
		$this->db->query("DELETE  FROM `tbl_arrange` WHERE arrange_id='{$arrange_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=arrange.HourdayList");
	}
	/** ----------------------------------学校作息时间---------------------------------------- **/
	/** 休息导出 **/
	public function actionRestOutData()
	{


		// 当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$hourrest_lists = $this->db->fetchAll("SELECT a.*,r.`resttype_name` FROM `tbl_arrange` AS a,`tbl_resttype` AS r WHERE a.arrange_type=4 AND a.resttype_id = r.resttype_id  ORDER BY arrange_id DESC ");

		foreach($hourrest_lists as $key => $value){

			$info = array(
			"0" => $value['resttype_name'],
			"1" => $value['arrange_content'],
			"2" => $value['start_time']."-".$value['end_time'],
			"3" => $value['arrange_remark'],
			);


			$data_lists[] = $info;


		}


		// 模板标题
		$this->model->excel->excel_ini['rest']['title'] = "作息时间安排";
		$this->model->excel->outData("rest", $data_lists);


	}




	/** 学校作息时间列表 **/
	public function actionHourRestlist ()
	{
		// 当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_arrange` WHERE arrange_type =4  ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$hourrest_lists = $this->db->fetchAll("SELECT a.*,r.`resttype_name` FROM `tbl_arrange` AS a,`tbl_resttype` AS r WHERE a.arrange_type=4 AND a.resttype_id = r.resttype_id  ORDER BY arrange_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("hourrest_lists", $hourrest_lists);
		$this->tpl->display('arrange/m.hourrest.list.tpl.php');
	}
	/** 学期作息时间添加 **/
	public function actionHourRestAdd()
	{
		// 当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 作息时间类别名称
		$resttype_lists = $this->db->fetchAll("SELECT `resttype_name`,`resttype_id` FROM `tbl_resttype`");

		if ($_POST){

			$arrange_title          = $this->post->arrange_title;
			$arrange_content   	    = $this->post->arrange_content;
			$arrange_remark   	    = $this->post->arrange_remark;
			$start_time     		= $this->post->start_time;
			$end_time     			= $this->post->end_time;
			$resttype_id     		= $this->post->resttype_id;
			$resttype_name     		= $this->post->resttype_name;

			// 自定义类别
			if(!empty($resttype_name)){
				$info = array(

				"resttype_name"         => $resttype_name,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_insert("tbl_resttype", $info);
				// 查找最后一行
				$resttype_lists = $this->db->fetchRow("SELECT resttype_id FROM `tbl_resttype` ORDER BY resttype_id DESC");
				$max_id = $resttype_lists['resttype_id'];


				$info = array(
				"semester_id"           => $semester_id,
				"arrange_content"   	=> $arrange_content,
				"arrange_remark"  		=> $arrange_remark,
				"arrange_type"  		=> 4,
				"resttype_id"  			=> $max_id,
				"start_time"  			=> $start_time,
				"end_time"  			=> $end_time,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_insert("tbl_arrange", $info);
			}else{
				// 写入数据库
				$info = array(
				"semester_id"           => $semester_id,
				"arrange_content"   	=> $arrange_content,
				"arrange_remark"  		=> $arrange_remark,
				"arrange_type"  		=> 4,
				"resttype_id"  			=> $resttype_id,
				"start_time"  			=> $start_time,
				"end_time"  			=> $end_time,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_insert("tbl_arrange", $info);
			}
			// 跳转
			Helper_Client::location("添加成功", "index.php?a=arrange.hourrestlist");
		}
		$this->tpl->assign("resttype_lists", $resttype_lists);
		$this->tpl->display('arrange/hourrest.add.tpl.php');
	}
	/** 学期作息时间修改 **/
	public function actionHourRestUpdate()
	{

		$arrange_id   = (int)$this->get->arrange_id;
		// 作息时间类别名称
		$resttype_lists = $this->db->fetchAll("SELECT `resttype_name`,`resttype_id` FROM `tbl_resttype`");
		$arrange_data = $this->db->fetchRow("SELECT * FROM `tbl_arrange` WHERE `arrange_id`='{$arrange_id}'");

		if (empty($arrange_data)){
			Helper_Client::alert("学期不存在");
		}

		if ($_POST){
            $resttype_id            = $this->post->resttype_id;
			$arrange_title          = $this->post->arrange_title;
			$arrange_content     	= $this->post->arrange_content;
			$arrange_remark   	    = $this->post->arrange_remark;
            $start_time   	    = $this->post->start_time;
            $end_time   	    = $this->post->end_time;

			// 写入数据库
			$info = array(
                "resttype_id"        => $resttype_id ,
            "start_time"            =>$start_time,
                "end_time"          =>    $end_time,
			"arrange_title"         => $arrange_title,
			"arrange_content"   	=> $arrange_content,
			"arrange_remark"  		=> $arrange_remark,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_arrange", $info, "arrange_id='{$arrange_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=arrange.hourrestlist");
		}
		$this->tpl->assign("resttype_lists", $resttype_lists);
		$this->tpl->assign("arrange_data", $arrange_data);
		$this->tpl->display('arrange/hourrest.update.tpl.php');
	}
	/** 学期作息时间删除 **/
	public function actionHourRestDel()
	{
		$arrange_id = $this->get->arrange_id;
		$this->db->query("DELETE  FROM `tbl_arrange` WHERE arrange_id='{$arrange_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=arrange.hourrestlist");
	}
	/** -----------------------------------周标准课时设置---------------------------------------- **/
	/** 周标准课时列表 **/
	public function actionHourList()
	{
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_hour`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$hour_lists = $this->db->fetchAll("SELECT c.class_name,s.subject_name,h.* FROM `tbl_subject` AS s,`tbl_class` AS c,`tbl_hour` AS h WHERE s.subject_id=h.subject_id AND c.class_id=h.class_id ORDER BY hour_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("hour_lists", $hour_lists);
		$this->tpl->display('hour/hour.list.tpl.php');
	}
	/** 周标准课时添加 **/
	public function actionHourAdd()
	{
		$subject_lists = $this->db->fetchAll("SELECT * FROM `tbl_subject`");
		$class_lists   = $this->db->fetchAll("SELECT * FROM `tbl_class`");

		if ($_POST){

			$hour_number      	= $this->post->hour_number;
			$subject_id      	= $this->post->subject_id;
			$class_id      		= $this->post->class_id;
			// 班级全部课时添加
			if($class_id == '0'){
				foreach($class_lists as $k=>$v){
					$class_row_id = $v['class_id'];

					$info = array(

					"hour_number"      	=> $hour_number,
					"subject_id"      	=> $subject_id,
					"class_id"      	=> $class_row_id,
					"status"            => 1,
					"create_time"       => date("Y-m-d H:i:s"),
					"update_time"       => "0000-00-00 00:00:00",

					);
					$this->db->do_insert("tbl_hour", $info);


				}
			}
			// 全部学科课时添加
			if($subject_id == '0'){
				foreach($subject_lists as $k=>$v){
					$subject_row_id = $v['subject_id'];

					$info = array(

					"hour_number"      	=> $hour_number,
					"subject_id"      	=> $subject_row_id,
					"class_id"      	=> $class_id,
					"status"            => 1,
					"create_time"       => date("Y-m-d H:i:s"),
					"update_time"       => "0000-00-00 00:00:00",

					);
					$this->db->do_insert("tbl_hour", $info);


				}
			}
			// 全部学科、全部班级课时添加
			if($subject_id == '0'&&$class_id == '0'){
				foreach($subject_lists as $k=>$v){
					$subject_row_id = $v['subject_id'];
					foreach($class_lists as $key=>$value){
						$class_row_id = $value['class_id'];

						$info = array(

						"hour_number"      	=> $hour_number,
						"subject_id"      	=> $subject_row_id,
						"class_id"      	=> $class_row_id,
						"status"            => 1,
						"create_time"       => date("Y-m-d H:i:s"),
						"update_time"       => "0000-00-00 00:00:00",

						);
						$this->db->do_insert("tbl_hour", $info);

					}
				}
			}

			// 写入数据库
			$info = array(

			"hour_number"      	=> $hour_number,
			"subject_id"      	=> $subject_id,
			"class_id"      	=> $class_id,
			"status"            => 1,
			"create_time"       => date("Y-m-d H:i:s"),
			"update_time"       => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_hour", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=arrange.hourlist");

		}
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('hour/hour.add.tpl.php');
	}
	/** 周标准课时修改 **/
	public function actionHourUpdate()
	{

		$hour_id	   = (int)$this->get->hour_id;
		$subject_lists = $this->db->fetchAll("SELECT `subject_id`,`subject_name` FROM `tbl_subject`");
		$class_lists   = $this->db->fetchAll("SELECT `class_id`,`class_name` FROM `tbl_class`");
		$hour_date	   = $this->db->fetchRow("SELECT * FROM `tbl_hour`  WHERE `hour_id`='{$hour_id}'");

		if (empty($hour_date)){
			Helper_Client::alert("课时不存在");
		}

		if ($_POST){

			$hour_number      	= $this->post->hour_number;
			$subject_id      	= $this->post->subject_id;
			$class_id      		= $this->post->class_id;

			// 写入数据库
			$info = array(

			"hour_number"      		=> $hour_number,
			"subject_id"      		=> $subject_id,
			"class_id"      		=> $class_id,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_hour", $info, "hour_id='{$hour_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=arrange.hourlist");
		}

		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("hour_date", $hour_date);
		$this->tpl->display('hour/hour.update.tpl.php');
	}
	/** 周标准课时删除 **/
	public function actionHourDel()
	{
		$hour_id = $this->get->hour_id;
		$this->db->query("DELETE  FROM `tbl_hour` WHERE hour_id='{$hour_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=arrange.hourlist");
	}
}

?>